﻿using DataBaseConnect;
using Domain;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.IO;

namespace CoreMVC.Jurisdiction.Manage
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //获取数据库连接字符串
            var sqlConnectionString = Configuration.GetConnectionString("sqlConnectionStrings");

            //添加链接字符串到上下文
            services.AddDbContext<MyDataBase>(options => options.UseSqlServer(sqlConnectionString));
           //options.UseSqlServer(connection, b => b.UseRowNumberForPaging());  // sql 2008 ef分页报错时使用
            //类注入
            Injection.InjectionMethod(services);
            services.AddMvc();

            //配置authorrize
            services.AddAuthentication(b =>
            {
                b.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                b.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                b.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            }).
              AddCookie(b =>
              {
                  //验证失败登陆地址
                  b.LoginPath = "/Login";
                  //sid
                  b.Cookie.Name = CookieAuthenticationDefaults.AuthenticationScheme;
                  // b.Cookie.Domain = "localhost";
                  b.Cookie.Path = "/";
                  b.Cookie.HttpOnly = true;
                  b.Cookie.Expiration = new TimeSpan(24, 0, 0);
                  b.ExpireTimeSpan = new TimeSpan(24, 0, 0);
                  b.DataProtectionProvider = DataProtectionProvider.Create(new DirectoryInfo(@"c:\shared-auth-ticket-keys\"));
                  // b.TicketDataFormat = ticketFormat;
              });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole();
            AppConfigurtaionServices.Configuration = this.Configuration;
            //登录验证
            app.UseAuthentication();

            if (env.IsDevelopment())
            {
                //开发环境异常处理
                //app.UseDeveloperExceptionPage();
                //生产环境异常处理
                app.UseExceptionHandler("/Shared/Error");
            }
            else
            {
                //生产环境异常处理
                app.UseExceptionHandler("/Shared/Error");
            }

            //使用静态文件
            app.UseStaticFiles();

            //使用Mvc，设置默认路由
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Login}/{action=Login}/{id?}");
            });

            //app.Run(async (context) =>
            //{
            //    await context.Response.WriteAsync("Hello World!");
            //});

            // 默认Home控制器的Index页面作为启动页
            // app.UseMvcWithDefaultRoute();
        }
    }
}
